package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.AccountManageService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.OperationRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.EditHistory;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.account.AccountManageModifyRequest;
import com.ktwlsoft.archivemanage.payload.account.AccountManageSaveRequest;
import com.ktwlsoft.archivemanage.payload.account.ResetUnitPasswordRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveEditHistoryRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 账号管理
 *
 * @author zhuhe on 2019/10/28 10:17
 * @version 1.0
 */
@Api(value = "账号管理", description = "账号管理")
@RestController
@RequestMapping("accountManage")
public class AccountManageController {

    /**
     * 账号管理
     */
    private final AccountManageService accountManageService;

    public AccountManageController(AccountManageService accountManageService) {
        this.accountManageService = accountManageService;
    }

    /**
     * 账号管理列表 on 2019/10/28 9:32
     *
     * @param keyWords keyWords
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "账号管理列表",tags = {"1.2"})
    @ResponseBody
    @GetMapping("findPage")
    @UserAuthenticate(actionCode = "ACCOUNT_PAGE", skip = true)
    public ResponseEntity<?> findPage(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize,
                                      @ApiParam(name = "keyWords", value = "关键字") @RequestParam(defaultValue = "", required = false) String keyWords) {
        return ResponseEntity.ok(accountManageService.findPage(pn, pageSize, keyWords));
    }

    /**
     * 账号管理详情 on 2019/10/28 9:32
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "账号管理详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("getById")
    @UserAuthenticate(actionCode = "ACCOUNT_DETAIL", skip = true)
    public ResponseEntity<?> getById(@ApiParam(name = "id", value = "账号主键") @RequestParam(defaultValue = "", required = false) Long id) {
        return ResponseEntity.ok(accountManageService.getById(id));
    }

    /**
     * 账号管理新增 on 2019/10/28 9:32
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "账号管理新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("save")
    @UserAuthenticate(actionCode = "ACCOUNT_ADD")
    public ResponseEntity<?> save(@Valid AccountManageSaveRequest request) {
        Result result = accountManageService.save(request);
        if (result.getResultType().equals(ResultType.BUSINESS_SUCCESS)) {
            saveLog(OperationLog.OperationType.ADD, "添加账号《" + request.getRealName() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 账号管理修改 on 2019/10/28 9:32
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "账号管理修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("modify")
    @UserAuthenticate(actionCode = "ACCOUNT_EDIT")
    public ResponseEntity<?> modify(@Valid AccountManageModifyRequest request) {
        Result result = accountManageService.modify(request);
        if (result.getResultType().equals(ResultType.BUSINESS_SUCCESS)) {
            SaveEditHistoryRequest historyRequest = new SaveEditHistoryRequest();
            historyRequest.setOldEntity(request.getOldValue());
            historyRequest.setNewEntity(request.getNewValue());
            historyRequest.setBusinessId(request.getId());
            historyRequest.setBusinessFrom(EditHistory.BusinessFrom.EDIT_ACCOUNT);
            saveLog(result, OperationLog.OperationType.EDIT, "编辑账号《" + request.getRealName() + "》", historyRequest);
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "重置密码", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "resetPassword")
    @UserAuthenticate(actionCode = "PASSWORD_RESET")
    public ResponseEntity<?> resetPassword(@Valid @RequestBody ResetUnitPasswordRequest request) {
        return ResponseEntity.ok(accountManageService.resetPassword(request));
    }

    /**
     * 账号管理删除 on 2019/10/28 9:32
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "账号管理删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("deleteById")
    @UserAuthenticate(actionCode = "ACCOUNT_DELETE")
    public ResponseEntity<?> deleteById(@ApiParam(name = "id", value = "账号主键") @RequestParam(defaultValue = "", required = false) Long id) {
        Result result = accountManageService.deleteById(id);
        if (result.getResultType().equals(ResultType.BUSINESS_SUCCESS)) {
            saveLog(OperationLog.OperationType.DELETE, "删除账号《" + result.getData() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 新增日志 on 2019/11/14 10:37
     *
     * @param operationType 操作类型
     * @param description   描述
     */
    private void saveLog(OperationLog.OperationType operationType, String description) {
        OperationRepositoryService.saveLog(new SaveLogRequest(
                operationType,
                description,
                "账号管理",
                OperationLog.BusinessType.SYSTEM
        ));
    }

    /**
     * 新增日志 on 2019/11/14 10:37
     *
     * @param operationType 操作类型
     * @param description   描述
     */
    private void saveLog(Result result, OperationLog.OperationType operationType, String description, SaveEditHistoryRequest editHistoryRequest) {
        OperationRepositoryService.saveLog(result, new SaveLogRequest(
                operationType,
                description,
                "账号管理",
                OperationLog.BusinessType.SYSTEM
        ), editHistoryRequest);
    }

    /**
     * 获取师资信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取师资信息", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/getTeacherInfo")
    public ResponseEntity<?> getTeacherInfo(@ApiParam(name = "id", value = "账号主键") @RequestParam(defaultValue = "", required = false) Long id) {
        return ResponseEntity.ok(accountManageService.getTeacherInfo(id));
    }


    /**
     * 师资信息修改
     *
     * @param updateRequest
     * @return
     */
    @ApiOperation(value = "师资信息修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("/updateIntroduce")
    public ResponseEntity<?> updateIntroduce(@ApiParam(name = "updateRequest", value = "师资信息更新参数，id:账号主键；introduce：老师介绍") @RequestBody Map updateRequest) {
        Object idObj = updateRequest.get("id");
        Object introduceObj = updateRequest.get("introduce");
        Long id = Long.valueOf(String.valueOf(idObj));
        String introduce = (String.valueOf(introduceObj));
        Result result = accountManageService.updateIntroduce(id, introduce);
        return ResponseEntity.ok(result);
    }

    /**
     * 获取所有业务指导员
     *
     * @return
     */
    @ApiOperation(value = "获取所有业务指导员", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/getInstructors")
    public ResponseEntity<?> getInstructors() {
        return ResponseEntity.ok(accountManageService.getInstructors());
    }

    /**
     * 获取所有业务指导员
     *
     * @return
     */
    @ApiOperation(value = "获取所有业务指导员", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/getAllInstructors")
    public ResponseEntity<?> getAllInstructors() {
        return ResponseEntity.ok(accountManageService.getAllInstructors());
    }
}